package de.easywakeup.gui;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import de.easywakeup.R;
import de.easywakeup.data.Data;
import de.easywakeup.yahooWeather.GetWoeid;

public class Settings extends Activity implements OnItemSelectedListener
{

	private Data data;

	private EditText timeBeforeMeetingET;
	private EditText timeAutoChangeET;
	private EditText location;
	private Spinner language;
	private static final String[ ] paths = { "English", "Deutsch" };
	private boolean isDefaultSelection;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_settings );

		data = Data.singleton( );
		// Initialize globals which are declared in the activity_settings.xml
		timeBeforeMeetingET = (EditText) findViewById( R.id.timeBeforeMeeting );
		timeBeforeMeetingET.setText( data.getTimeBeforeMeeting( ) );
		timeAutoChangeET = (EditText) findViewById( R.id.timeAutoChange );
		timeAutoChangeET.setText( data.getTimeAutoChange( ) );
		location = (EditText) findViewById( R.id.location );
		location.setText( data.getLocation( ) );
		language = (Spinner) findViewById( R.id.language );
		language.setSelection( data.getLanguage( ) );

		// Is necessary for Clickevent on the language drop down field
		ArrayAdapter< String > adapter = new ArrayAdapter< String >( Settings.this, android.R.layout.simple_spinner_item, paths );
		adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
		// Connect an adapter for updating the List on runtime
		language.setAdapter( adapter );
		// Set ClickListener
		language.setOnItemSelectedListener( this );
		// Set true at onCreate, because the on item selected fires the event on
		// initialisation
		isDefaultSelection = true;

	}

	public void declineButtonListener( View v )
	{

		System.out.println( "declineButtonListener" );
		timeBeforeMeetingET.setText( "" );
		timeAutoChangeET.setText( "" );
		location.setText( "" );
		language.setSelection( 0 );
		Intent intent = new Intent( this, MainScreen.class );
		startActivity( intent );

	}

	public void okButtonListener( View v )
	{
		// Save entered Data in the Datamodel
		System.out.println( "okButtonListener" );

		// Check if the enterd digit a number

		// Check if the enterd digit a number
		if( isNumeric( timeBeforeMeetingET.getText( ).toString( ) ) )
		{
			data.setTimeBeforeMeeting( timeBeforeMeetingET.getText( ).toString( ) );
		}
		else
		{
			makeToast( getString( R.string.enterNumeric ) );
		}

		// Check if the enterd digit a number
		if( isNumeric( timeAutoChangeET.getText( ).toString( ) ) )
		{
			data.setTimeAutoChange( timeAutoChangeET.getText( ).toString( ) );
		}
		else
		{
			makeToast( getString( R.string.enterNumeric ) );
		}

		data.setLocation( location.getText( ).toString( ) );

		// Weather Query
		Data.setGetWoeid( new GetWoeid( ) );
		if( location.getText( ).toString( ).equals( "" ) )
		{
			makeToast( getString( R.string.enterPlace ) );
		}
		else
		{
			Data.getGetWoeid( ).startQuery( );
			Data.getGetWeather( ).startQuery( data.getWoeid( ) );
		}
		Intent intent = new Intent( this, MainScreen.class );
		startActivity( intent );
	}

	public void searchOnClickListener( View v )
	{

		if( location.getText( ).toString( ).equals( "" ) )
		{
			makeToast( getString( R.string.enterPlace ) );
		}
		else
		{
			data.setLocation( location.getText( ).toString( ) );
			Data.setGetWoeid( new GetWoeid( ) );
			Data.getGetWoeid( ).startQuery( );
		}
	}

	@Override
	public void onItemSelected( AdapterView< ? > parent, View v, int position, long id )
	{
		if( isDefaultSelection )
		{ // If spinner initializes
			language.setSelection( data.getLanguage( ) );
			isDefaultSelection = false;
		}
		else
		{ // If user manually select item

			// store selection (itemPosition) of user into data
			data.setLanguage( language.getSelectedItemPosition( ) );

			// Set different language
			Locale locale = new Locale( "" );
			switch( position)
			{
				case 1:
					locale = new Locale( "de" );
					break;

				default:

					break;
			}
			Locale.setDefault( locale );
			Configuration config = new Configuration( );
			config.locale = locale;
			getBaseContext( ).getResources( ).updateConfiguration( config, getBaseContext( ).getResources( ).getDisplayMetrics( ) );
		}

	}

	@Override
	public void onNothingSelected( AdapterView< ? > arg0 )
	{
		// TODO Auto-generated method stub

	}

	private void makeToast( String message )
	{
		Toast.makeText( getBaseContext( ), message, Toast.LENGTH_LONG ).show( );

	}

	public static boolean isNumeric( String str )
	{
		try
		{
			Integer.parseInt( str );
		}
		catch( NumberFormatException nfe )
		{
			return false;
		}
		return true;
	}

	public Data getData( )
	{
		return data;
	}

	public void setData( Data data )
	{
		this.data = data;
	}

	public EditText getLocation( )
	{
		return location;
	}

	public void setLocation( EditText location )
	{
		this.location = location;
	}

}
